package com.example.supplychainbackend.entity.purchase;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.util.concurrent.atomic.AtomicInteger;

@Getter
@Setter
@Entity
@Table(name = "purchase_require")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PurchaseRequireEntity {
    @Id
    private String purchaseRequestNumber;

    private String purchaseRequestDepartment;
    private String lineNumber;
    private Integer purchaseType;
    private String workOrderNumber;
    private String purchaseGroup;
    private String contractName;
    private String contractNumber;
    private String salesOrderName;
    private String salesOrderNumber;
    private String materialNumber;
    private String materialName;
    private String materialDescription;
    private String supplierCode;
    private String supplierName;
    private Integer requiredQuantity;
    private Integer orderedQuantity;
    private String materialCategory;
    private String materialSubCategory;
    private String plannedArrivalDate;
    private String orderDate;
    private String requiredDeliveryDate;
    private String materialSource;
    private String mrpType;
    private String remarks;
    private String purchaseUnit;
    private String status;
    private Integer approvalStatus;
    private String unitOfMeasure;
    private String leadTime;
    private String requestSource;
    private String company;
    private String factory;
    private String manufacturer;
    private String oldMaterialNumber;
    private String customerMaterialCode;
    private String manufacturerMaterialCode;
    private String suggestedOrderDate;
    private Boolean convertibleToOrder;
    private String substituteMaterialType;
    private String substituteGroup;
    private String recentApprovalTime;
    private String plannedReceivingLocation;
    private Integer printCount;
    private String applicant;
    private String department;
    private Float priceBeforeTax;

    @CreationTimestamp
    private String createDate;

    private String file;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @Transient
    private static AtomicInteger globalCounter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (this.purchaseRequestNumber == null || this.purchaseRequestNumber.isEmpty()) {
            // 原子递增获取序列号
            int sequence = globalCounter.getAndIncrement();

            // 格式化为PR+YYYYMMDD+五位序号
            this.purchaseRequestNumber = "PR" + DateTimeUtils.getCurrentDateForID() + String.format("%05d", sequence);
        }
    }
}
